import { connect } from 'dva';
import Cookies from 'universal-cookie';
const cookie = new Cookies();

/**
 *  版本兼容,以前项目方法
 * */

/**
 * Math.pow(x,y) : x-底数, y-幂数
 * 根据十进制数数组 => 该数组的2的N次幂的十进制数的总和
 * */
export function conversionIntAll(array){
  //最多65
  var number = 0;
  for (var i = 0; i < array.length; i++){
    var item = parseInt(array[i]);
    number += Math.pow(2, item);
  }
  return number;
}
/**
 * id关联
 * 十进制数 + 数组 => 十进制数所代表的文字字符串
 * */
export function conversionTextStation(textNum, comArray) {
  var text = "";
  var textInt = parseInt(textNum);
  var twoNum = textInt.toString(2);
  var array = twoNum.split("");

  if (textInt == 0) {
    text = "";
  } else {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == "1") {
        var add = "";
        if (i > 0) {
          add = ", "
        }
        var id = array.length - i - 1;
        for (var j = 0; j < comArray.length; j++){
          if (id == parseInt(comArray[j].id)){
            text = comArray[j].stationLocal + add + text;
          }
        }
      }
    }
  }
  return text;
}
/**
 * id关联
 * 10进制数+数组 => 10进制所代表的数据的id数组
 * */
export function conversionTextId(textNum, comArray) {
  var text = [];
  var textInt = parseInt(textNum);
  var twoNum = textInt.toString(2);
  var array = twoNum.split("");
  if (textInt == 0) {
    text = [];
  } else {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == "1") {
        var id = array.length - i - 1;
        for (var j = 0; j < comArray.length; j++){
          if (id == parseInt(comArray[j].value)){
            text.push(comArray[j].value + "");

          }
        }
      }
    }
  }
  return text;
}
/**
 * @param textNum
 * @param textOnly
 * @param comArray
 * @returns {string}
 * 十进制数 + 数组 => 十进制数所代表的文字字符串
 */
export function conversionText(textNum, comArray) {
  var text = "";
  var textInt = parseInt(textNum);
  var twoNum = textInt.toString(2);
  var array = twoNum.split("");

  if (textInt == 0) {
    text = "";
  } else {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == "1") {
        var add = "";
        if (i > 0) {
          add = ", "
        }
        text = comArray[array.length - 1 - i] + add + text;
      }

    }
  }
  return text;
}

/**
 * 数字转化成2进制,
 * @param textNum
 * @returns {Array}
 * 十进制数转二进制数数组
 */
export function conversionArray(textNum) {
  var array = [];
  var textInt = parseInt(textNum);

  var twoNum = textInt.toString(2);
  var dataArr = twoNum.split("");
  for (var i = dataArr.length - 1; i >= 0; i--) {
    array.push(dataArr[i]);
  }
  return array;
}
/**
 * 单选树{label, key, value}根据id取值
 * */
export function conversionTextById(id, array){
  var text = "";
  for (var i = 0; i < array.length; i++){
    if (id + "" == array[i].value){
      text = array[i].label;
    }
  }
  return text;
}

/**
 * 多选树{label, key, value}根据id取值
 * */
export function conversionTextMoreTree(textNum, comArray) {
  var text = "";
  var textInt = parseInt(textNum);
  var twoNum = textInt.toString(2);
  var array = twoNum.split("");
  if (textInt == 0) {
    text = "";
  } else {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == "1") {
        var add = "";
        if (i > 0) {
          add = ", "
        }
        var id = array.length - i - 1;
        for (var j = 0; j < comArray.length; j++){
          if (id == parseInt(comArray[j].value)){
            text = comArray[j].label + add + text;
          }
        }
      }
    }
  }
  return text;
}

/**
 * @param 亚仙单选数
 */
export function conversionTitleByValue(value, array){
  var text = "";
  for (var i = 0; i < array.length; i++){
    if (value + "" == array[i].value){
      text = array[i].title;
      break;
    }
  }
  return text;
}
